package com.shinechain.twitter;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CallbackServlet {
    private static final long serialVersionUID = 1657390011452788111L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(request.getSession());
        Twitter twitter = (Twitter) request.getSession().getAttribute("twitter");
        RequestToken requestToken = (RequestToken) request.getSession().getAttribute("requestToken");
        String verifier = request.getParameter("oauth_verifier");
        //String verifier = "ykGHyc1w49UVLYF9Vt2giOsv4u9QnUEA";
        try {

            AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
            request.getSession().removeAttribute("requestToken");
            ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.setOAuthConsumerKey("ipp4zV5GKKUncWGmbRAFeV3Yb");
            builder.setOAuthConsumerSecret("OZmBI2ZdJaqGMXwE46QI0RnrSUJj7h2WRTMWuENCn2Mm9Hh0OZ");
            builder.setOAuthAccessToken(accessToken.getToken());
            builder.setOAuthAccessTokenSecret(accessToken.getTokenSecret());
            builder.setIncludeEmailEnabled(true);
            User user = twitter.verifyCredentials();
            System.out.print(user);
            // User user = twitter.showUser(accessToken.getUserId());
            System.out.println(user);
        } catch (TwitterException e) {
            throw new ServletException(e);
        }
        response.sendRedirect(request.getContextPath() + "/");
    }

}
